package com.ymatou.counter.api.testcase;

import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.counter.api.parameter.getSingleBean;
import com.ymatou.counter.api.service.getSingleCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.CounterCallService;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 获取计数 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_getSingle {
	private static getSingleBean ssb;
	private static getSingleCall ssc;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取计数");
	}

	@Before
	public void caseUp() {
		ssb = new getSingleBean();
		ssc = new getSingleCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(ssc.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_getSingle_001() {
		Logger.start(true,"获取单个计数成功");
		try {
			String TargetId = TestDataManager.getRandomUUID().toString();
			int value1 = new Random().nextInt(5) + 1;
			Logger.comment("给TargetId为"+TargetId + ":添加计数器 c1: "+value1);
			new CounterCallService().setSingle("Test", TargetId, 1, value1);
			Thread.sleep(3000);
			ssb.setAppId("Test");
			ssb.setTargetId(TargetId);
			ssb.setCounter("c1");
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(ECode.SUCCESS, ssc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.SUCCESSMSG3, ssc.getMsg(), "验证Msg");
			Logger.verifyEquals(value1, ssc.getData(), "验证TargetId("+TargetId+")的计数器的值");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_getSingle_002() {
		Logger.start(false,"不输入AppId,失败");
		try {
			String TargetId = TestDataManager.getRandomUUID().toString();
			ssb.setTargetId(TargetId);
			ssb.setCounter("c1");
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(ECode.ERROR400, ssc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.NOAPPID_MSG, ssc.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_getSingle_003() {
		Logger.start(false,"不输入TargetId,失败");
		try {
			ssb.setAppId("Test");
			ssb.setCounter("c1");
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(ECode.ERROR400, ssc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.NOTARGETID_MSG, ssc.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_getSingle_004() {
		Logger.start(false,"不输入Counter,失败");
		try {
			String TargetId = TestDataManager.getRandomUUID().toString();
			ssb.setAppId("Test");
			ssb.setTargetId(TargetId);
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(ECode.ERROR400, ssc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.NOCOUNTER_MSG, ssc.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_getSingle_005() {
		Logger.start(false,"输入错误的AppId,失败");
		try {
			String TargetId = TestDataManager.getRandomUUID().toString();
			ssb.setAppId("errTest");
			ssb.setTargetId(TargetId);
			ssb.setCounter("c1");
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(ECode.ERROR500, ssc.getCode(), "验证Code");
			Logger.verifyIsContains(ECode.NONODE_MSG, ssc.getMsg(), "验证Msg包含信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_getSingle_006() {
		Logger.start(false,"输入错误的Counter,失败");
		try {
			String TargetId = TestDataManager.getRandomUUID().toString();
			ssb.setAppId("Test");
			ssb.setTargetId(TargetId);
			ssb.setCounter("errc1");
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(ECode.ERROR500, ssc.getCode(), "验证Code");
			Logger.verifyIsContains(ECode.NONODE_MSG, ssc.getMsg(), "验证Msg包含信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_getSingle_007() {
		Logger.start(false,"输入不存在的TargetId,没有查询出数据");
		try {
			String TargetId = TestDataManager.getRandomUUID().toString();
			ssb.setAppId("Test");
			ssb.setTargetId(TargetId);
			ssb.setCounter("c1");
			ssc.setData(ssb);
			ssc.callService();
			Logger.verifyEquals(ECode.SUCCESS, ssc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.SUCCESSMSG3, ssc.getMsg(), "验证Msg");
			// 输入不存在的TargetId 查询出的计数器都是0
			Logger.verifyEquals(0, ssc.getData(), "验证TargetId（"+TargetId+")的计数器的值");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}